// pages/my/stockGoodsSet/stockGoodsSet.js
var app = getApp()
Page({
  data: {
    // showView:false,
    putInCart:false,
    good:{},
    currentBox:[],
    goodType: "2",
    goodTypes:[],
    versionValueIds:'',
    productId:'1',
    ifAddUnderLineGoods:false,
    salePrice:'',
    isStock:true
  },
  onLoad: function (options) {
    if (options.price) {
      this.setData({
        ifAddUnderLineGoods:true,
        salePrice:options.price
      })
    }
    this.setData({
      productId:options.id,
      type:options.type
    })
    var that = this
    app.getMemInfo().then(res=>{
      let agentId = res.data.agentId
      let accountId = res.data.id
      this.setData({
        agentId,
        accountId
      })
      this.getGoodInfo(agentId)
      this.getGoodVersion()
    })

  },
  //获取商品详情
  getGoodInfo(agentId){
    var that = this
    let data = {
      agentId,
      productId:this.data.productId,
      type:this.data.type
    }
    console.log(data)
    app.request('wx_app/agent/stock/product/detail',data,'GET').then(res=>{
      console.log(res)
      let good = {
        name:res.data.name,
        url:res.data.url,
        stock:res.data.stock,
        purchasPriceAvg:200,
        purchasPrice:'',
        purchasNum:'',
        price:res.data.price,
        imgs:res.data.details
      }
      that.setData({
        good
      })
    })
  },
  //获取商品规格
  getGoodVersion(){
    const that = this
    let data = {
      productId:this.data.productId,
      type:this.data.type
    }
    app.request('wx_app/product/version/list',data,'GET').then(res=>{
      console.log(res)
      let attr = res.data.versions
      let goodTypes = []
      attr.forEach(item=>{
        goodTypes.push({
          name:item.name,
          infos:item.content
        })
      })
      that.setData({
        goodTypes
      })
      //获取默认规格
      let currentBox = []
      let versions = []
      attr.forEach(item=>{
        currentBox.push({
          name:item.name,
          infos:item.content[0]
        })
      })
      let goodType = ''
      currentBox.forEach(item=>{
        goodType += ' '+item.infos.name
      })
      currentBox.forEach(item=>{
        versions += item.infos.id+','
      })
      that.setData({
        currentBox,
        goodType,
        versionValueIds:versions.slice(0,versions.length-1),
      })
    })
  },
  chooseAttr(e){
    let name = e.currentTarget.dataset.attrName
    let id = e.currentTarget.dataset.attrId
    let index = e.currentTarget.dataset.index
    let currentBox = this.data.currentBox
    console.log(e)
    currentBox.forEach((item,i)=>{
      if (i==index) {
        item.infos.name = name
        item.infos.id = id
      }
    })
    let goodType = ''
      currentBox.forEach(item=>{
        goodType += ' '+item.infos.name
      })
    this.setData({
      currentBox,
      goodType
    })
    this.getProductStockInfo()
  },
  //根据规格获取库存及价格
  getProductStockInfo(){
    var that = this
    let versions = []
    this.data.currentBox.forEach(item=>{
      console.log(item)
      versions += item.infos.id+','
    })
    console.log(versions)
    let data = {
      type:this.data.type,
      productId:this.data.productId,
      agentId:this.data.agentId,
      versionValueIds:versions.slice(0,versions.length-1),
    }
    console.log(data)
    app.request('wx_app/agent/stock/productStockInfo',data,'GET').then(res=>{
      console.log(res)
      that.setData({
        ['good.stock']:res.data.stock,
        ['good.price']:res.data.price,
        versionValueIds:versions.slice(0,versions.length-1)
        // ['good.purchasPrice']:res.data.stockInPrice,
        // ['good.purchasNum']:res.data.stockInCount
      })
    })
  },
  saveGoodSet(){
    let versions = []
    this.data.currentBox.forEach(item=>{
      versions += item.infos.id+','
    })
    console.log(versions)
    if (this.data.ifAddUnderLineGoods) {
      this.savePrice(versions)
    }else{
      this.saveStock(versions)
    }
  },
  savePrice(versions){
    let data = {
      type:this.data.type,
      productId:this.data.productId,
      versionValueIds:versions.slice(0,versions.length-1),
      agentId:this.data.agentId,
      price:this.data.good.price
    }
    console.log(data)
    app.request('wx_app/agent/stock/saveProductStockSetting',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        console.log(777)
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000,
          success(){
            setTimeout(function(){
              wx.navigateBack({
                delta:1
              })
            },1000)
            
          }
        })
      }
    })
  },
  saveStock(versions){
    console.log(versions)
    let data = {
      productId:this.data.productId,
      productType:this.data.type,
      versionValueIds:versions.slice(0,versions.length-1),
      accountId:this.data.accountId,
      agentId:this.data.agentId,
      price:this.data.good.purchasPrice,
      stock:this.data.good.purchasNum
    }
    console.log(data)
    app.request('wx_app/agent/stock/flow/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000,
          success(){
            setTimeout(function(){
              wx.navigateBack({
                delta:1
              })
            },1000)
            
          }
        })
        
      }
    })
  },
  changePrice(e){
    this.setData({
      ['good.price']:e.detail.value
    })
  },
  purchasNum(e){
    this.setData({
      ['good.purchasNum']:e.detail.value
    })
  },
  purchasPrice(e){
    this.setData({
      ['good.purchasPrice']:e.detail.value
    })
  },
  selectGTap:function(){
    this.setData({
      showView: false
    })
  },
  selectMoreGTap:function(){
    this.setData({
      showView: true
    })
  },
  closeBtnView:function(){
    this.setData({
      showView: false
    })
  },
  stockDetails:function(){
    let id = this.data.productId
    let type = this.data.type
    let ver = this.data.versionValueIds
    wx.navigateTo({
      url: '../stockDetails/stockDetails?id='+id+'&type='+type+'&ver='+ver
    })
  },
  editPrice(e){
    this.setData({
      ['good.price']:e.detail.value
    })
  },
  add(e){
    let num = e.currentTarget.dataset.num
    num++
    this.setData({
      ['good.purchasNum']:num
    })
  },
  desc(e){
    let num = e.currentTarget.dataset.num
    if (num>0) {
      num--
    }
    this.setData({
      ['good.purchasNum']:num
    })
  },
  deletePrice(){
    this.setData({
      ['good.price']:0,
      salePrice:0
    })
  }
})